package com.yc.forum.servlet;

import com.yc.forum.dao.PostDao;
import com.yc.forum.util.Utils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static redis.clients.jedis.search.IndexDefinition.Type.JSON;

//分页 分类查询 帖子id  index页面
@WebServlet(name = "ClassifyQureyPostServlet", value = "/ClassifyQureyPost.s")
public class ClassifyQureyPostServlet extends HttpServlet {
    private PostDao postdao=new PostDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String module=request.getParameter("module");
        String page=request.getParameter("page");
        String size=request.getParameter("size");
        String search=request.getParameter("search");
        Map<String,Object> pagelist= postdao.selectClassify(module,page,size,search);

        //-------------------------------------------------------------------------------------------------------
        System.out.println("---------------------init ClassifyQureyPostServert-------------------------");

        //取到body的值，并去除<p></p>标签
        List<Map<String,String>> body=getText(""+pagelist.get("list"));
        System.out.println("body="+body.toString());
        //--------------------------------------------------------------------------------------------------------
        /*去除其他标签*/
        List<Map<String,String>> newbody=new ArrayList<>();
        for(int i=0;i<body.size();i++){
            Map<String,String> map=body.get(i);
            String text=map.get("text");
            text=Utils.takeOutHtmlTab(text);
            map.put("text", text);

            newbody.add(map);
            System.out.println("map="+map.toString());
        }
        pagelist.put("newbody",newbody);
        System.out.println("pagelist="+pagelist.toString());
        Utils.toJson(pagelist,response);
    }



    public List<Map<String,String>> getText(String mainbody){

        System.out.println("--------init getText------------");
        List<Map<String,String>> texts=new ArrayList<>();
        int beginIndex=0;
        while(true) {
            //返回 <p> 字符第一次出现的字符串内的索引，以指定的索引开始搜索。
            String strings = ", body=<p>";
            int Begin = mainbody.indexOf(strings, beginIndex) + strings.length()-1;
//            System.out.println(mainbody);
//            System.out.println(mainbody.indexOf(strings, beginIndex));
//            System.out.println("Begin="+Begin);
            //如果未找到img标签，则结束循环
            if(Begin<(strings.length()-1)){
                break;
            }
            int End = mainbody.indexOf("</p>, problemkey=", Begin);
//            System.out.println("End="+End);
            //取到 img 标签 src中的内容
            String ImgText = mainbody.substring(Begin + 1, End);
//            System.out.println(ImgText);
            //将该次img end索引赋值给beginindex，作为下一次查询的起始点
            beginIndex = End;
            //截取字符串在前台展示一部分
            if(ImgText.length()>=300)
                ImgText=ImgText.substring(0,300);
            //将开始位置，结束位置，内容记录
            Map<String,String> map=new HashMap<>();
            map.put("begin",""+Begin);
            map.put("end",""+End);
            map.put("text",""+ImgText);
            texts.add(map);
        }
        return texts;
    }
}
